layui.use(top.layuiuse,
    function () {
        var admin = layui.admin;
        var popup = layui.popup;
        var form = layui.form;
        var dropdown = layui.dropdown;
        var kaPublic = layui.kaPublic;
        var watermark = layui.watermark;
        var kaLayer = layui.kaLayer;
        var kaHtmlStatic = layui.kaHtmlStatic;
        var kaAjax = layui.kaAjax;
        var kaBgUrl = layui.kaBgUrl;
        var kaLanguage = layui.kaLanguage;
        var $ = layui.jquery;
        var languageTypeDivId = "";// 语言类型渲染Id
        var layuiFormId = "";
        var dropdownDataJson = [];
        var adminConfigurationPath = "";
        //公共
        kaPublic.initBodySetUp();
        //加载水印
        loadingWatermark(watermark);
        //请求后台配置
        kaAjax.ajax(kaBgUrl.url("homeSetUp"), function (rs) {
            if (rs.code == 200) {
                languageTypeDivId = rs.data.languageTypeDivId; // 语言类型渲染Id
                layuiFormId = rs.data.layuiFormId;
                dropdownDataJson = rs.data.dropdownDataJson;
                adminConfigurationPath = rs.data.adminConfigurationPath;
            }
        })
        // topUsername
        var kaCurrentUser = layui.kaCurrentUser.get().data;
        top.currentUser = kaCurrentUser;
        $("#topUsername").html(kaCurrentUser.nickName);
        $("#usermenuurl_usernameImg").attr("src", kaCurrentUser.avatar);
        //系统应用
        kaAjax.ajax(kaBgUrl.url("getAppById"), function (rs) {
            if (rs.code == 200) {
               $("#systemAppClass").html(rs.data.name);
            }
        }, {id: kaCurrentUser.mainSysApp});
        $(".systemApp").click(function (params) {
            var kaAjaxFormData = {};
            kaAjax.ajax(kaBgUrl.url("getTheApp"), function (rs) {
                if (rs.code == 200) {
                    var data = rs.data;
                    var timeApp = data.timeApp;
                    var contentApp = data.contentApp;
                    var title = kaHtmlStatic.v1(timeApp);
                    var content = kaHtmlStatic.v11({ apps: contentApp });
                    kaLayer.v1({
                        area: ['100%', '400px'],
                        title: title,
                        content: `<div id="currentTableId" lay-filter="currentTableFilter"></div>`,
                        success: function (params) {
                            var currentTable = layui.cardTable.render({
                                elem: '#currentTableId',
                                page: false,
                                data: contentApp,
                                linenum: 6
                            })
                        }
                    });
                }
            }, kaAjaxFormData);
        });
        // 获取语言类型
        kaLanguage.renderingOption("languageSelect", function (rs) {
            if (rs.code == 200) {
                var languageTypeData = rs.data;//语言类型数据
                $("#" + languageTypeDivId).html(kaPublic.loadOptionHtml(languageTypeData, "id", "name"));
                form.render();
                var languageTypeCurrent = localStorage.getItem("kaLanguageTypeCurrent");
                if (languageTypeCurrent == undefined || languageTypeCurrent == null || languageTypeCurrent == "" || languageTypeCurrent == "null") {
                    languageTypeCurrent = 1;
                }
                form.val(layuiFormId, { "language": languageTypeCurrent });
                form.on('select(' + languageTypeDivId + ')', function (data) {
                    var value = data.value; // 获得被选中的值
                    localStorage.setItem("kaLanguageTypeCurrent", value);
                    kaLanguage.loadingValue(value);
                    window.location.reload();
                });
            }
        });
        // topUsername
        var kaCurrentUser = layui.kaCurrentUser.get().data;
        top.currentUser = kaCurrentUser;
        $("#topUsername").html(kaCurrentUser.nickName);
        $("#usermenuurl_usernameImg").attr("src", kaCurrentUser.avatar);
        // 下来菜单
        dropdown.render({
            elem: '#usermenuurl',
            data: kaHtmlStatic.v2(dropdownDataJson),
            click: function (data, othis, event) {
                if (data.id == 1) {
                    layui.kaLayer.v2({
                        title: kaHtmlStatic.v21(layui.kaCurrentUser.get().data),
                        content: kaHtmlStatic.v22(layui.kaCurrentUser.get().data),
                    });
                } else if (data.id == 2) {
                    layer.open({
                        id: "formRenderingHtml2",
                        type: 2,
                        title: '修改密码',
                        shadeClose: true,
                        shade: [0.6, '#FFF'],
                        area: ['480px', '480px'],
                        content: './formRendering.html?id=2' // iframe 的 url
                    });
                } else if (data.id == 3) {
                    KaUltraAdmin.changePage({ id: "999", title: "账户信息/管理", type: "_iframe", url: "kaAccountInfo.html" });
                } else if (data.id == 4) {
                    KaUltraAdmin.changePage({ id: "1000", title: "我的小工具", type: "_component", url: "view/kaMySmallTool/index.html" });
                } else if (data.id == 5) {
                    KaUltraAdmin.changePage({ id: "1000", title: "帮助文档", type: "_iframe", url: "https://docs.dingtalk.com/i/desktop" });
                } else if (data.id == 6) {
                    layui.kaMsg.error("敬请期待功能开发中", "功能开发中");
                } else if (data.id == 7) {
                    layui.kaMsg.error("敬请期待功能开发中", "功能开发中");
                } else if (data.id == 8) {
                    layui.kaMsg.error("敬请期待功能开发中", "功能开发中");
                } else if (data.id == 9) {
                    layui.kaMsg.error("敬请期待功能开发中", "功能开发中");
                } else if (data.id == 10) {
                    layui.kaAjax.ajax(layui.kaBgUrl.url("monitorOnlineForceLogout"), function (rs) { // rs 返回的驗證碼 JSON
                        if (rs.code == 200) {
                             layui.kaCurrentUser.get();
                        } else {
                            layui.kaMsg.error(rs.msg, JSON.stringify(rs));
                        }
                    }, {"tokenId": layui.kaCurrentUser.get().token});
                }
            }
        });
        // yml | json | api
        admin.setConfigurationPath(adminConfigurationPath);
        // 渲染
        admin.render();
        // 注销
        admin.logout(function () {
            popup.success("注销成功", function () {
                location.href = "./login.html";
            })
            // 清空 tabs 缓存
            return new Promise((resolve) => { resolve(true) });
        });
    }
)
// 测试环境
function testAmbient(id) {
    alert(id);
}
// 正式环境
function formalAmbient(id) {
    alert(id);
}
// 添加应用
function addApp(params) {
    // 打开应用
    window.open('https://layui.dev/', '_blank');
}